# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response, get_object_or_404
from django.http import Http404
from django.template import RequestContext
from django.contrib.auth.decorators import user_passes_test
from getdebt.main.models import Firm, Debt
from getdebt.advice.views import advices
from getdebt.profile.views import userinfo, user_can_view
from django.forms.models import modelformset_factory
import datetime

ABC=u'АБВГДЕЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ'

def alf(request):
    #список из пары значений буква, число !!!
    abc=[]
    for i in enumerate(ABC):
    	abc.append(i)
    return {'abc':abc}

def abc_filter(list,field, strpos):
    #Возвращает queryset начинающийся на букву strpos
    try:
    	pos=int(strpos)
    except:
    	pos=0
    abc=alf(0)['abc'][pos][1]
    if field=='name':
        try:
            new_list = list.filter(name__startswith=abc)
        except:
            new_list = list    
    if field=='fio':
        try:
            new_list = list.filter(fio__startswith=abc)
        except:
            new_list = list    
    return new_list 

def obj_model_html(obj,exclude=[]):
    #возвращает список из пары значений поле-значение для объекта модели
    d=[]
    fields = obj._meta.fields
    for f in fields:
        if not f.name in exclude:
            val=obj.__getattribute__(f.name)
            if val==True:
                val='Да'
            elif val==False:
                val='Нет'
            d.append([f.verbose_name,val])     
    return d    
        
    
def main(request):
    dictionary={}
    dictionary['ext_tpl']='main-2col.tpl'    
    return render_to_response('maintext.tpl', dictionary,
        context_instance=RequestContext(request, processors=[userinfo]))
    
def html_pages(request, page):
    try:
        return render_to_response('html/%s.html' % page, 
        context_instance=RequestContext(request, processors=[userinfo]))
    except TemplateDoesNotExist:
        raise Http404()
